# coding:utf-8
import os
import argparse


def compare(dirA, dirB, dirC, args):
    A_items = os.listdir(dirA)
    B_items = os.listdir(dirB)

    if not os.path.exists(dirC):
        os.makedirs(dirC)

    A_items.sort()
    B_items.sort()
    N = min(len(A_items), len(B_items))

    # 构建调用 PngCompare.py 的批处理sh脚本
    cmdFmt = 'python PngCompare.py --A "%s/%s" --B "%s/%s" --C "%s/%s"'
    cmdList = [ \
                cmdFmt % (dirA, A_items[k], dirB, B_items[k], dirC, A_items[k] + '_--_' + B_items[k] + '_-_.png')
                for k in range(N)
              ]
    cmdStrs = '\n'.join(cmdList) + '\n'

    with open('Tmp.sh', 'wb') as fid:
        fid.write(cmdStrs.encode('utf-8'))

    # 并发执行批处理sh脚本里的命令
    runStr = 'python mProcRun.py --shPath Tmp.sh --poolType %s --workers %d' % \
             (args.poolType, args.workers)
    if args.logPath is not None:
        runStr += ('  --logPath %s' % args.logPath)
    print(runStr)
    os.system(runStr)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--A', help='src dir A')
    parser.add_argument('--B', help='src dir B')
    parser.add_argument('--C', help='dst dir C')
    parser.add_argument('--poolType',
                        default='P',
                        help='T(Thread) or P(Process) or N(None), (default P)')
    parser.add_argument('--workers', default=4, type=int, help='(default 4)')
    parser.add_argument('--logPath')
    args = parser.parse_args()

    compare(args.A, args.B, args.C, args)
